package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void delete(Long id) {
        //删除关系
        roleMapper.deleteRelation(id);

        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Role role,Long[] ids) {
        roleMapper.insert(role);
        if (ids != null && ids.length > 0){
            roleMapper.insertBatchRelation(role.getId(),ids);
        }
    }

    @Override
    public Role selectById(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        return role;
    }

    @Override
    public List<Role> selectAll() {
        List<Role> roles = roleMapper.selectAll();
        return roles;
    }

    @Override
    public void update(Role role, Long[] ids) {
        roleMapper.updateByPrimaryKey(role);

        roleMapper.deleteRelation(role.getId());

        if (ids != null && ids.length > 0){
            roleMapper.insertBatchRelation(role.getId(),ids);
        }

    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> roles = roleMapper.selectForList(qo);

        return new PageInfo<>(roles);
    }
}
